#/bin/bash
#Auto Mount LVM for CentOS
#更新时间2022年8月27日

echo "it-arch Auto LVM for CentOS"
echo
echo "########################################"
echo "Operating risk, only use new Cloud    ##"
echo "Press Ctrl + C to cancel              ##"
echo "Any key to continue                   ##"
echo "########################################"
read -n 1 
vgName=`vgdisplay | grep 'VG Name' | awk '{print $NF}'`
echo "VG Name:"$vgName
lvPath=`lvdisplay  | awk 'NR==2 {print $NF}'`
echo "LV Path:"$lvPath

#Create lv disk
#/dev/xvdb 可以修改为真实的磁盘名称如：/dev/sdb
fdisk /dev/xvdb <<EOF
n
p
1


t
8e
w
EOF
pvcreate /dev/xvdb1

#Add LV to VG
vgextend $vgName /dev/xvdb1

#Find VG Free Size
vgSize=`vgdisplay | grep 'Free  PE / Size' |cut -d / -f 2| awk '{print $(NF)}'`
#vgFreeSizeUnit=`vgdisplay | grep 'Free  PE / Size' | awk '{print $(NF)}'`
#Add LV Size
lvresize -l  +$vgSize  $lvPath
resize2fs $lvPath

#Show LV Size
echo
echo "#########################################"
echo "                                       ##"
echo " The operation completed! Please check ##"
echo "                                       ##"
echo "#########################################"
echo Disk Size:`lvdisplay | grep "LV Size" | awk '{print $(NF-1)}' | head -n 1`GB

rm -rf AutoMountLVM.sh
